import {Injectable} from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {environment} from '../../../environments/environment';

@Injectable({
  providedIn: 'root'
})
export class B2bService {
  serviceUrls: any = {
    dictUrl: '/dict/detail', // 字典查询
    uploadFileUrl: '/common/upload/file', // 上传

    orderList: '/b2bOrder/list', // 订单列表
    storeList: '/b2bOrder/storeList', // 店铺列表
    exportOrder: '/b2bOrder/getOrderExcel', // 订单导出

    orderStatistics: '/b2bOrder/count', // 订单统计

    track: '/follower/save', // 跟进新增
    trackList: '/follower/list', // 跟进列表
  };

  constructor(public httpClient: HttpClient) {
  }

  // 文件上传
  fileUpload(file: any) {
    return this.post(this.serviceUrls.uploadFileUrl, file).toPromise();
  }

  // 跟进
  track(body: any) {
    return this.post(this.serviceUrls.track, body).toPromise();
  }

  // 跟进列表
  getTrackList(body: any) {
    return this.post(this.serviceUrls.trackList, body).toPromise();
  }

  getDict(key: string) {
    return this.httpClient.get('/dict/details?key=' + key, {}).toPromise();
  }

  // 获取订单列表
  getOrderList(body: any) {
    return this.post(this.serviceUrls.orderList, body).toPromise();
  }

  // 获取店铺列表
  getStoreList() {
    return this.post(this.serviceUrls.storeList, {}).toPromise();
  }

  // 订单导出
  exportOrder(params: any) {
    return this.httpClient.get(this.serviceUrls.exportOrder, {params, responseType: 'blob'}).toPromise();
  }

  // 获取订单统计列表
  getOrderStatisticsList(body: any) {
    return this.post(this.serviceUrls.orderStatistics, body).toPromise();
  }

  get(url: any, params: any) {
    return this.httpClient.get(url, {params});
  }

  post(url: any, body: any) {
    return this.httpClient.post(url, body);
  }

  formatTime(date: any, type: 'date' | 'dateTime' | 'time') {
    if (!date || (typeof date) === 'string') {
      return date || '';
    }
    let result = '';
    switch (type) {
      case 'date':
        // @ts-ignore
        result = date.toISOString().match(/\d{4}-\d{2}-\d{2}/)[0];
        break;
      case 'dateTime':
        // @ts-ignore
        result = date.toISOString().match(/\d{4}-\d{2}-\d{2}/)[0] + ' ' + date.toString().match(/\d{2}:\d{2}:\d{2}/)[0];
        break;
      case 'time':
        // @ts-ignore
        result = date.toString().match(/\d{2}:\d{2}:\d{2}/)[0];
        break;
    }
    return result;
  }

}
